package com.sunlands.qdcy.deskmate.web;

import com.sunlands.qdcy.deskmate.common.annotation.AutoLog;
import com.sunlands.qdcy.deskmate.config.RedisConfig;
import com.sunlands.qdcy.deskmate.enums.PayType;
import com.sunlands.qdcy.deskmate.vo.BusinessResult;
import com.sunlands.qdcy.deskmate.vo.PayTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author anjl
 */

@Slf4j
@RestController
@RequestMapping("/pay/manage")
@Api(tags = {"支付管理"}, description = "支付管理")
public class PayManagerController extends BaseController {

    @Autowired
    private RedisTemplate<String, List<PayTypeVO>> redisTemplate;

    @AutoLog
    @GetMapping("/android")
    @ApiOperation(value = "APP调用-安卓支付方式", response = PayTypeVO.class)
    public BusinessResult<List<PayTypeVO>> payType() {
        List<PayTypeVO> list = redisTemplate.opsForValue().get(RedisConfig.CacheKeys.ANDROID_TYPE);
        if (Objects.isNull(list) || list.isEmpty()) {
            list = Arrays.asList(PayTypeVO.builder().name(PayType.ALIPAY.getName()).code(PayType.ALIPAY.name()).build(),
                    PayTypeVO.builder().name(PayType.WECHAT.getName()).code(PayType.WECHAT.name()).build());
            redisTemplate.opsForValue().set(RedisConfig.CacheKeys.ANDROID_TYPE, list);

        }
        return BusinessResult.createSuccessInstance(list);
    }

    @AutoLog
    @PreAuthorize("isAuthenticated()")
    @PutMapping("/android")
    @ApiOperation(value = "修改安卓支付方式", response = PayTypeVO.class)
    public BusinessResult<List<PayTypeVO>> payType(@RequestBody List<PayTypeVO> list) {
        if (Objects.nonNull(list) && !list.isEmpty()) {
            redisTemplate.opsForValue().set(RedisConfig.CacheKeys.ANDROID_TYPE, list);
        }
        return BusinessResult.createSuccessInstance(list);
    }


}